//
//  EeBottomToggleView.swift
//  soapp
//
//  Created by zhu xietong on 2018/5/11.
//

import Foundation
import Eelay

open class EeBottomToggleView: JoView {    
    
    public weak var container:UIView?
    
    open var backView:UIView?{
        get{
            if let v = container{
                return v
            }
            return UIApplication.shared.keyWindow
        }
    }
    
    
    var sb_constraints = [NSLayoutConstraint]()
    
    public func show(animated:Bool=true) {
        if let backV = backView{
//            self.translatesAutoresizingMaskIntoConstraints = false
//            backV.addSubview(self)
//            let t = NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: backV, attribute: .top, multiplier: 1, constant: topOffSet)
//
//            let l = NSLayoutConstraint(item: self, attribute: .left, relatedBy: .equal, toItem: backV, attribute: .left, multiplier: 1, constant: 0)
//
//            let b = NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: backV, attribute: .bottom, multiplier: 1, constant: 0)
//
//            let r = NSLayoutConstraint(item: self, attribute: .right, relatedBy: .equal, toItem: backV, attribute: .right, multiplier: 1, constant: 0)
            let constrains = backV.setEeLays(lays: [
                [self,[ee.L.B.R],[ee.T.safe]]
            ]).1
            backV.addConstraints(constrains)
            sb_constraints = constrains
            
            if animated{
            self.alpha = 0
            weak var wself = self

            UIView.animate(withDuration: 0.20, animations: {
                wself?.alpha = 1
            }) { (finish) in
                
            }
            }
            else{
                self.alpha = 1
            }
            

        }
    }
    
    public func hidden(animated:Bool=true) {
        weak var wself = self
        self.alpha = 1
        UIView.animate(withDuration: 0.20, animations: {
            wself?.alpha = 0
        }) { (finish) in
            guard let ws = wself else{return}
            ws.backView?.removeConstraints(ws.sb_constraints)
            ws.removeFromSuperview()
        }
        
    }
    
    
}
